using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Jupiter.Infrastructure;
using Jupiter.Infrastructure.Exceptions;
using Jupiter.Infrastructure.Security;

namespace Wips.Domain.Models.Authorities.Model
{
    /// <summary>
    /// 用户
    /// </summary>
    [Table("AuthorityUsers")]
    [SugarTable("AuthorityUsers")]
    public class User : EntityBase, IAggregateRoot
    {

        /// <summary>
        /// 用户名
        /// </summary>
        [Required]
        [MaxLength(32)]
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        [MaxLength(16)]
        public string RealName { get; set; }

        /// <summary>
        /// 手机号
        /// </summary>
        [MaxLength(11)]
        public string PhoneNumber { get; set; }

        /// <summary>
        /// Email
        /// </summary>
        /// <value></value>
        [MaxLength(256)]
        [EmailAddress]
        public string? Email { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public Status Status { get; set; }

        // /// <summary>
        // /// 用户类别
        // /// </summary>
        // public UserTypeEnum UserType { get; set; }




        /// <summary>
        /// 最后更新人Id
        /// </summary>
        [MaxLength(36)]
        public string LastUpdatedUserId { get; set; }

        /// <summary>
        /// 用户组织架构
        /// </summary>
        [Navigate(NavigateType.OneToMany, nameof(OrganizationUser.UserId))]
        public List<OrganizationUser> OrganizationUsers { get; set; }


        public User()
        {
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="realName">真实姓名</param>
        /// <param name="phoneNumber">手机号</param>
        /// <param name="email">电子邮箱</param>
        /// <param name="status">状态</param>

        /// <param name="creatorId">创建人</param>
        public User(string userName, string password, string realName, string phoneNumber, string email, Status status,
        string creatorId)
        {
            UserName = userName;
            Password = PasswordHash.CreateHash(password);
            RealName = realName;
            PhoneNumber = phoneNumber;
            Email = email;
            Status = status;
            // UserType = userType;
            CreatorId = creatorId;
            LastUpdatedUserId = creatorId;
            Id = ObjectId.GenerateNewStringId();

        }


        /// <summary>
        /// 状态变更
        /// </summary>
        /// <param name="lastUpdatedUserId"></param>
        public void StatusChange(string lastUpdatedUserId)
        {
            Status = Status == Status.Disabled ? Status.Enabled : Status.Disabled;
            LastUpdatedUserId = lastUpdatedUserId;
            Updated = DateTime.Now;

            // 状态为启用时
            if (Status == Status.Enabled)
            {
                return;
            }

            // 不能禁用开发者帐号
            if (UserName == "root")
            {
                throw new CoreException("不能禁用开发者帐号。");
            }

            // 添加领域事件
            //AddDomainEvent(new UserDisabledEvent(Id));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="password"></param>
        /// <param name="realName"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="email"></param>
        /// <param name="lastUpdatedUserId"></param>
        /// <param name="isRoot"></param>
        public void Update(
            string userName, string password, string realName, string phoneNumber,
            string email, string lastUpdatedUserId, bool isRoot)
        {
            // 密码为空时不修改
            if (!string.IsNullOrEmpty(password))
            {
                Password = PasswordHash.CreateHash(password);
            }

            // 非开发者帐号不能修改开发者帐号的密码
            if (UserName == "root" && !isRoot && !string.IsNullOrEmpty(password))
            {
                throw new CoreException("非开发者帐号不能修改开发者帐号的密码");
            }

            //UserName = userName;
            RealName = realName;
            //PhoneNumber = phoneNumber;
            Email = email;
            LastUpdatedUserId = lastUpdatedUserId;
            Updated = DateTime.Now;
            // UserType = userType;

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="realName"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="email"></param>
        /// <param name="lastUpdatedUserId"></param>
        public void UpdateBasic(string realName, string phoneNumber, string email, string lastUpdatedUserId)
        {
            RealName = realName;
            PhoneNumber = phoneNumber;
            Email = email;
            LastUpdatedUserId = lastUpdatedUserId;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <param name="lastUpdatedUserId"></param>
        public void ChangePassword(string oldPassword, string newPassword, string lastUpdatedUserId)
        {
            if (!PasswordHash.ValidatePassword(oldPassword, Password))
            {
                throw new CoreException("当前密码输入有误");
            }

            Password = PasswordHash.CreateHash(newPassword);
            LastUpdatedUserId = lastUpdatedUserId;
        }
    }
}